﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Domain;
using Domain.Entity;
using Spider.Spiders;
using Spider.Spiders.Workers;
using UUWiseCSWrapper;
using QqWBSprider = Spider.Spiders.Workers.QqWBSprider;

namespace Spider.Service
{
    /// <summary>
    /// 爬虫模块入口服务
    /// </summary>
    public static partial class SpiderService
    {
        /// <summary>
        /// 添加Url到ToDo队列
        /// </summary>
        /// <param name="urls"></param>
        /// <returns></returns>
        public static bool AddUrlToTodoQueue(string[] urls)
        {
            try
            {
                foreach (var url in urls)
                {
                    if (!string.IsNullOrWhiteSpace(url))
                    {
                        ToDoData urData = new ToDoData();
                        urData.PageUrl = url;
                        urData.DataSource = Spider.Spiders.Spider.GetPageDataSource(url);
                        ToDo.AddUrl(urData);
                    }

                }
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("添加Url到ToDo队列出错", ex);
                return false;
            }
        }

        /// <summary>
        /// 添加内容类型数据添加到ToDo队列
        /// </summary>
        /// <param name="content"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool AddContentToTodoQueue(string[] content,string type)
        {
            try
            {
                foreach (var c in content)
                {
                    if (!string.IsNullOrWhiteSpace(c))
                    {
                        ToDoData urData = new ToDoData();
                        urData.PageUrl = c;
                        urData.DataSource = Spider.Spiders.Spider.GetContentDataSource(type);
                        ToDo.AddUrl(urData);
                    }

                }
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("添加内容类型数据添加到ToDo队列出错", ex);
                return false;
            }
        }
        /// <summary>
        /// 获取爬虫队列中的URLs
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAllToTodoQueueUrls()
        {
            List<string> list=new List<string>();
            var quen=ToDo.GetAll();
            foreach (ToDoData toDoData in quen)
            {
                string url = toDoData.PageUrl;
                list.Add(url);
            }
            return list;
        }
        /// <summary>
        /// 获取爬虫队列中的URLs
        /// </summary>
        /// <returns></returns>
        public static void GetAllToTodoQueueUrls(List<string> urls )
        {
            foreach (string url in urls)
            {
                ToDo.AddUrl(new ToDoData()
                {
                    
                });
            }
        }
        /// <summary>
        /// 命令蜘蛛队长开始工作
        /// </summary>
        /// <returns></returns>
        public static bool CommandSpiderToWork()
        {
            try
            {
                Thread th = new Thread(SpiderCaptain.GetInstance().StartToWork);
                th.Start();
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("命令蜘蛛队长开始工作出错", ex);
                return false;
            }
        }
        /// <summary>
        /// 命令蜘蛛队长开始工作，,单线程模型
        /// </summary>
        /// <returns></returns>
        public static bool CommandSpiderToWorkBySTA(WebBrowser browser)
        {
            try
            {
                SpiderCaptain.GetInstance().StartToWorkBySTA(browser);
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("命令蜘蛛队长开始工作出错", ex);
                return false;
            }
        }
        /// <summary>
        /// 命令蜘蛛队长停止工作
        /// </summary>
        /// <returns></returns>
        public static bool CommandSpiderStopToWork(string reason = "")
        {
            try
            {
                SpiderCaptain.GetInstance().StopToWork(reason);
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("命令蜘蛛队长停止工作出错", ex);
                return false;
            }
        }
        /// <summary>
        /// 设置QQ爬虫的属性值
        /// </summary>
        /// <param name="param"></param>
        /// <param name="cookie"></param>
        public static void SetQqSpriderAttribute(string param, string cookie)
        {
            QqSprider.Param = param;
            CookieContainer myCookieContainer = new CookieContainer();
            string[] cookstr = cookie.Split(';');
            foreach (string str in cookstr)
            {
                string[] cookieNameValue = str.Split('=');
                Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().Replace(",", "%2C").ToString());
                ck.Domain = "g.cnc.qzone.qq.com"; //"user.qzone.qq.com";
                myCookieContainer.Add(ck);
            }
            QqSprider.Cookie = myCookieContainer;
        }
        /// <summary>
        /// 设置爬虫队长工作速度
        /// </summary>
        /// <param name="speed"></param>
        public static void SetSpriderCaptainWorkSpeed(int speed)
        {
            SpiderCaptain.WorkSpeed = speed;
        }
       
        /// <summary>
        /// 设置爬虫WebBrowser
        /// </summary>
        /// <param name="browser"></param>
        public static void SetSpriderWebBrowser(WebBrowser browser)
        {
            Spiders.Spider.WebBrowser = browser;
        }
        /// <summary>
        /// 设置爬虫WebBrowser延时
        /// </summary>
        /// <param name="second"></param>
        public static void SetSpriderWebBrowserDelay(int second)
        {
            Spiders.Spider.WebBrowserDelay = second;
        }
        #region  QQ空间爬虫设置
        /// <summary>
        /// 设置Qq空间爬虫验证码
        /// </summary>
        /// <param name="second"></param>
        public static void SetQqSpriderVerifyCode(string code)
        {
            QqSprider.VerifyCode = code;
        }
        /// <summary>
        /// 设置Qq空间爬虫启用时光轴留痕
        /// </summary>
        /// <param name="second"></param>
        public static void SetQqSpriderEnableTimeVisit(bool enable)
        {
            QqSprider.EnableTimeLineVisite = enable;
        }
        /// <summary>
        /// 设置Qq空间爬虫每个账号最后访问指定地址
        /// </summary>
        /// <param name="url"></param>
        public static void SetQqSpriderVisiteDestinationUrl(string url)
        {
            QqSprider.VisiteDestinationQQ = url;
        }
        /// <summary>
        /// 设置Qq空间爬虫自动识别验证码
        /// </summary>
        /// <param name="auto"></param>
        public static void SetQqSpriderAutoWritVerifyCode(bool auto)
        {
            QqSprider.AutoWritVerifyCode = auto;
        }
        /// <summary>
        /// 设置Qq空间爬虫自动识别验证码允许单个账号最大错误识别次数
        /// </summary>
        /// <param name="times"></param>
        public static void SetQqSpriderAutoWritVerifyCodeErrorTimes(int times)
        {
            QqSprider.AutoWritVerifyCodeErrorTimes = times;
        }
        /// <summary>
        /// 设置Qq空间爬虫自动申请访问空间
        /// </summary>
        /// <param name="apply"></param>
        public static void SetQqSpriderApplyVisiteZone(bool apply)
        {
            QqSprider.ApplyVisiteZone = apply;
        }
        /// <summary>
        /// 设置Qq空间爬虫自动申请空间加好友
        /// </summary>
        /// <param name="add"></param>
        public static void SetQqSpriderZoneAddFriend(bool add)
        {
            QqSprider.ZoneAddFriend = add;
        }
        /// <summary>
        /// 设置Qq空间爬虫自动空间说说点赞
        /// </summary>
        /// <param name="say"></param>
        public static void SetQqSpriderZoneShuoShuoSayGood(bool say)
        {
            QqSprider.ShuoShuoSayGood = say;
        }
        /// <summary>
        /// 设置Qq空间爬虫自动空间首页点赞
        /// </summary>
        /// <param name="say"></param>
        public static void SetQqSpriderZoneIndexSayGood(bool say)
        {
            QqSprider.IndexSayGood = say;
        }
        /// <summary>
        /// 设置Qq空间爬虫自动留言板留言
        /// </summary>
        /// <param name="live"></param>
        public static void SetQqSpriderZoneMessageBorderLiveMessage(bool live)
        {
            QqSprider.ZoneMessageBorderLiveMessage = live;
        }
        /// <summary>
        /// 设置Qq空间爬虫自动日志赞
        /// </summary>
        /// <param name="say"></param>
        public static void SetQqSpriderEnableZoneLogsSayGood(bool say)
        {
            QqSprider.EnableZoneLogsSayGood = say;
        }
        /// <summary>
        /// 设置Qq空间虫账号日志是否可以访问
        /// </summary>
        /// <param name="visit"></param>
        public static void SetQqSpriderCheckZoneLogsAvaliable(bool visit)
        {
            QqSprider.CheckZoneLogsAvaliable = visit;
        }
        /// <summary>
        /// QQ空间更换验证码
        /// </summary>
        public static void ChangeVerifyCode()
        {
            new QqSprider().ChangeVerifyCode();
        }
        #endregion
        #region  QQ微博爬虫设置
        /// <summary>
        /// 设置Qq微博单个账号访问
        /// </summary>
        /// <param name="enable"></param>
        public static void SetQqWbSpriderOneAcountAccess(bool enable)
        {
            QqWBSprider.EnableOneAcountAccess = enable;
        }
        /// <summary>
        /// 设置Qq微博爬虫验证码
        /// </summary>
        /// <param name="second"></param>
        public static void SetQqWbSpriderVerifyCode(string code)
        {
            QqWBSprider.VerifyCode = code;
        }
        /// <summary>
        /// 设置Qq微博爬虫每个账号最后访问指定地址
        /// </summary>
        /// <param name="url"></param>
        public static void SetQqWbSpriderVisiteDestinationQQ(string url)
        {
            QqWBSprider.VisiteDestinationQQWB = url;
        }
        /// <summary>
        /// 设置Qq微博爬虫自动识别验证码
        /// </summary>
        /// <param name="auto"></param>
        public static void SetQqWbSpriderAutoWritVerifyCode(bool auto)
        {
            QqWBSprider.AutoWritVerifyCode = auto;
        }
        /// <summary>
        /// 设置Qq微博爬虫自动识别验证码允许单个账号最大错误识别次数
        /// </summary>
        /// <param name="times"></param>
        public static void SetQqWbSpriderAutoWritVerifyCodeErrorTimes(int times)
        {
            QqWBSprider.AutoWritVerifyCodeErrorTimes = times;
        }
        /// <summary>
        /// 设置Qq微博爬虫自动申请空间加好友
        /// </summary>
        /// <param name="add"></param>
        public static void SetQqWbSpriderAddFriend(bool add)
        {
            QqWBSprider.EnableAddFriend = add;
        }
        /// <summary>
        /// 设置Qq微博爬虫自动空间首页点赞
        /// </summary>
        /// <param name="say"></param>
        public static void SetQqWbSpriderSayGood(bool say)
        {
            QqWBSprider.EnableSayGood = say;
        }
        /// <summary>
        /// 设置Qq微博爬虫启用搜索用户访问
        /// </summary>
        /// <param name="search"></param>
        public static void SetQqWbSpriderSearchUser(bool search)
        {
            QqWBSprider.EnableSearchUser = search;
        }
        /// <summary>
        /// QQ微博更换验证码
        /// </summary>
        public static void QqWbChangeVerifyCode()
        {
            new QqWBSprider().ChangeVerifyCode();
        }
        #endregion
        /// <summary>
        /// 清除IE浏览器缓存
        /// </summary>
        public static void ClearBrowserFileByRunCmd()
        {
           SpiderHelper.ClearBrowserFileByRunCmd();
        }
        /// <summary>
        /// 重置爬虫系统
        /// </summary>
        public static void ReSetSpiderConfig()
        {
            SpiderCaptain.EndTime = DateTime.MinValue;
            SpiderCaptain.StopWorkReason ="";
        }
        /// <summary>
        /// Uu云验证码识别
        /// </summary>
        /// <param name="picContent"></param>
        /// <returns></returns>
        public static string Uu_recognizeByCodeTypeAndBytes(byte[] picContent)
        {
            if (picContent == null)
                return "";
            StringBuilder result = new StringBuilder(50);
            Wrapper.uu_recognizeByCodeTypeAndBytes(picContent, picContent.Length, 3004, result);
            var temp= result.ToString();
            string[] ts = temp.Split('_');
            return ts.Last();
        }
    }
}
